<template>
  <el-container>
    <el-header>
      <div class="left-box">
        <div class="logo">
          <img
            src="https://img1.baidu.com/it/u=1653443696,112616033&fm=26&fmt=auto&gp=0.jpg"
            alt=""
          />
        </div>

        <h1>大鹏后台管理系统</h1>
      </div>
      <el-button type="info" @click="loginOut">退出登录</el-button></el-header
    >
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="isCollapse = !isCollapse">|||</div>
        <el-menu
          :default-active="currentActive"
          background-color="#333744"
          text-color="#FFFFFF"
          class="el-menu-vertical-demo"
          v-if="menusList"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          router
        >
          <el-submenu
            :index="data.id + ''"
            v-for="data in menusList"
            :key="data.id"
          >
            <template slot="title" class="head-title">
              <i :class="iconsObj[data.id]"></i>
              <span>{{ data.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + item.path"
              v-for="item in data.children"
              :key="item.id"
              @click="itemClick('/' + item.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item.authName }}</span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>
<script>
import { Container, Header, Aside, Main, Button, Menu, Submenu, MenuItem } from 'element-ui'
import Vue from 'vue'
Vue.use(Container).use(Header)
  .use(Aside).use(Main).use(Button)
  .use(Menu).use(Submenu).use(MenuItem)
export default {
  methods: {
    async getMenuData() {
      const { data: res } = await this.$http.get('menus');
      if (res.meta.status !== 200) return this.$message.erro(res.meta.msg)
      this.menusList = res.data;
    },
    itemClick(path) {
      window.sessionStorage.setItem('currentActive', path);
      this.currentActive = path;
    },
    loginOut() {
      this.$confirm('您确认退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
        window.sessionStorage.clear();
        this.$router.push('/login');
      }).catch(() => {
      });
    }
  },
  data() {
    return {
      menusList: [],
      isCollapse: false,
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-opportunity',
        101: 'el-icon-s-data',
        102: 'el-icon-s-management',
        145: 'el-icon-s-order'
      },
      currentActive: ''
    }
  },
  mounted() {
    this.getMenuData();
    this.currentActive = window.sessionStorage.getItem('currentActive')
  }
}
</script>
<style lang="scss" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  align-items: center;
  color: white;
  justify-content: space-between;
  .left-box {
    display: flex;
    align-items: center;
    h1 {
      margin-left: 50px;
    }
    .logo {
      margin-left: 50px;
      width: 60px;
      height: 60px;
      border-radius: 50%;
      img {
        border-radius: 50%;
        height: 100%;
        width: 100%;
      }
    }
  }
}
.el-aside {
  background: #333744;
  .toggle-button {
    background: #4a5064;
    color: white;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    // 字体之间的间距
    letter-spacing: 0.2em;
  }
}
.head-title {
  width: 100%;
}
.el-menu {
  border: none;
}
.el-main {
  background: #eaedf1;
}
</style>
